package com.nitty.cash.loop.ui.presenter

import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyApiUrl.addCommonParams
import com.nitty.cash.loop.event.NittyEventPointHelper
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：校验 Presenter 实现
 */
class CheckPresenter(private val iView: IViewContract.ICheckView) : CommonPresenter(iView) {

    //选择类型  SMS  、 WHATSAPP
    private var selectType = ""
    //是否需要选择出生日期
    private var needSubmitBirthDay = "NO"

    fun getCheckNumberSelectType(): String{
        return selectType
    }

    /**
     * 是否需要验证生日
     */
    fun getCheckNumberNeedSubmitBirthDay(): String{
        return needSubmitBirthDay
    }

    /**
     * 切换校验类型
     */
    fun setCheckNumberType(type: String,isSend: Boolean = true){
        if (selectType == type){
            return
        }
        selectType = type
        iView.showNittyCheckType(type)
        if (isSend){
            sendCheckNumberWhatsAppCodeAction()
        }
    }

    /**
     * 发送WhatsApp验证码
     */
    fun sendCheckNumberWhatsAppCodeAction(){
        //发送验证，埋点增加手机号
        NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_VERIFICATION_GETCODE,iView.getNittyCheckMobile())
        iView.showNittyLoading()
        val loginMobile = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_WHATSAPP_SEND_CODE_PATH)
            put("la2066c657", loginMobile)
            put("y1812e138ae14d3f", iView.getNittyCheckMobile())
            put("e8a2",selectType)
        }
        val httpUrl = NittyApiUrl.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //是否需要验证生日
                    if (obj.has("needSubmitBirthDay")){
                        needSubmitBirthDay = obj.optString("needSubmitBirthDay")
                    }
                    //当为YES时可以跳过，直接调用流程接口，根据流程步骤whatsappFlag标识走
                    val directLoginFlag = obj.optString("directLoginFlag")
                    iView.showNittyCheckSendCodeSuccess(directLoginFlag)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    /**
     * 校验验证码
     */
    fun onCheckNumberVerificationCodeAction(submitBirthDay: String = ""){
        //需要提交出生日期
        if (needSubmitBirthDay == "YES" && submitBirthDay.isEmpty()){
            iView.showNittyCheckChooseBirthdayDialog()
            return
        }
        iView.showNittyLoading()
        val loginMobile = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_WHATSAPP_CHECK_CODE_PATH)
            put("la2066c657", loginMobile)
            put("y1812e138ae14d3f", iView.getNittyCheckMobile())
            put("g557ac09",iView.getNittyCheckCode())
            put("z4bf4",getCheckNumberSelectType())
            put("l492cc528b5a4",needSubmitBirthDay)//是否需要验证生日
        }
        if (needSubmitBirthDay == "YES" && submitBirthDay.isNotEmpty()){
            params.put("j266",submitBirthDay)//出生日期
        }
        val httpUrl = NittyApiUrl.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenNittyLoading()
                iView.showNittyCheckSuccess()
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }
}